package es.decloud.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.Arrays;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;

@Entity
@Table(name = "tutor")
@Indexed
public class Tutor extends BaseObject implements Serializable {

	private static final long serialVersionUID = -5833313445157419340L;

	private Long id;
	private Usuario usuario;
	private Alumno alumno;
	private byte[] foto;
	private String dni;
	private boolean avisoEmail;
	private boolean avisoSms;
	private boolean parteEmail;

	public Tutor() {
	}

	public Tutor(Alumno alumno, boolean avisoEmail, boolean avisoSms, boolean parteEmail) {
		this.alumno = alumno;
		this.avisoEmail = avisoEmail;
		this.avisoSms = avisoSms;
		this.parteEmail = parteEmail;
	}

	public Tutor(Usuario usuario, Alumno alumno, byte[] foto, String dni, boolean avisoEmail, boolean avisoSms,
			boolean parteEmail) {
		this.usuario = usuario;
		this.alumno = alumno;
		this.foto = foto;
		this.dni = dni;
		this.avisoEmail = avisoEmail;
		this.avisoSms = avisoSms;
		this.parteEmail = parteEmail;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	@DocumentId
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name = "usuario_id")
	public Usuario getUsuario() {
		return this.usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	@ManyToOne
	@JoinColumn(name = "alumno_id", nullable = false)
	public Alumno getAlumno() {
		return this.alumno;
	}

	public void setAlumno(Alumno alumno) {
		this.alumno = alumno;
	}

	@Column(name = "foto")
	public byte[] getFoto() {
		return this.foto;
	}

	public void setFoto(byte[] foto) {
		this.foto = foto;
	}

	@Column(name = "dni", length = 20)
	@Field
	public String getDni() {
		return this.dni;
	}

	public void setDni(String dni) {
		this.dni = dni;
	}

	@Column(name = "aviso_email", nullable = false)
	public boolean isAvisoEmail() {
		return this.avisoEmail;
	}

	public void setAvisoEmail(boolean avisoEmail) {
		this.avisoEmail = avisoEmail;
	}

	@Column(name = "aviso_sms", nullable = false)
	public boolean isAvisoSms() {
		return this.avisoSms;
	}

	public void setAvisoSms(boolean avisoSms) {
		this.avisoSms = avisoSms;
	}

	@Column(name = "parte_email", nullable = false)
	public boolean isParteEmail() {
		return this.parteEmail;
	}

	public void setParteEmail(boolean parteEmail) {
		this.parteEmail = parteEmail;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((alumno == null) ? 0 : alumno.hashCode());
		result = prime * result + ((dni == null) ? 0 : dni.hashCode());
		result = prime * result + ((usuario == null) ? 0 : usuario.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Tutor other = (Tutor) obj;
		if (alumno == null) {
			if (other.alumno != null)
				return false;
		}
		else if (!alumno.equals(other.alumno))
			return false;
		if (dni == null) {
			if (other.dni != null)
				return false;
		}
		else if (!dni.equals(other.dni))
			return false;
		if (usuario == null) {
			if (other.usuario != null)
				return false;
		}
		else if (!usuario.equals(other.usuario))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).append("usuario", usuario).append("alumno", alumno)
				.append("foto", Arrays.toString(foto)).append("dni", dni).append("avisoEmail", avisoEmail)
				.append("avisoSms", avisoSms).append("parteEmail", parteEmail).toString();
	}

}
